<?
	include("includes/config.php");
	include("includes/login.php");
	include("includes/functions.php");
	$nr_comanda = $_GET['nr_comanda'];
	$nr_inmatc = $_GET['nr_inmat'];
	$id_comanda = $_GET['id_comanda'];
	if(@$_POST['add_articol']){
		$articol = $_POST['articol'];
		$cnt = $_POST['cnt'];
		$codarticol = $_POST['codarticol'];
		$pretU = $_POST['pretu'];
		$tvaFlag = 1;
		
		
		
		$res_ch = mysql_query("select cod_articol from comenzi_materiale where cod_articol = '$codarticol'  ");
		if(mysql_num_rows($res_ch) < 1){
			mysql_query("insert into
			comenzi_materiale (
				id_material,
				cod_articol,
				denumire,
				pret,
				tva,
				stoc
			)values(
				NULL,
				'$codarticol',
				'$articol',
				'$pretU',
				'$tvaFlag',
				'$cnt'
			)") or die("query inser in materiale" .mysql_error());
		}
		
		$res_cod = mysql_query("select * from comenzi_materiale where cod_articol = '$codarticol' ") or die(mysql_error());
		$rowmat = mysql_fetch_array($res_cod);
		$id_mat = $rowmat['id_material'];
		
		$ch_rel = mysql_query("select * from comenzi_materiale_relatii where id_comanda = '$id_comanda' and id_material = '$id_mat' ");
		if(mysql_num_rows($ch_rel) >= 1 ){
			mysql_query("update comenzi_materiale_relatii set cant = cant +  '$cnt'  where id_material = '$id_mat'");
		}else{
			mysql_query("insert into comenzi_materiale_relatii (id_material_rel,id_material,id_comanda,cant) values(null,'$id_mat','$id_comanda','$cnt')   ")or die( "query insert in relati materiale" .mysql_error());
		}

	}

	
	
	
	if(@$_GET['stergeMaterial']){
		$id_mat = $_GET['stergeMaterial'];
		mysql_query("delete from comenzi_materiale_relatii where id_material = '$id_mat' and id_comanda ='$id_comanda' ");
		#mysql_query("update comenzi_materiale set id_comanda = 0 where id_material = '$id_mat' ");
	}
	if(@$_POST['sterge_art']){
		$cod = $_POST['codarticol'];
		mysql_query("delete from comenzi_materiale where cod_articol = '$cod' ");
	}

?>
<html>
	<head>
		<link rel="stylesheet" type="text/css" href="css/style.css" />
	</head>
	<body>
		<div id="header"><h1> <? echo detaliFirma('nume_firma'); ?></h1></div>
		<div id="warper">
			<?
				$resMeniu = mysql_query("select * from meniu");
			?>
			<div id="menu">
				<ul>
					<? while($rowMen = mysql_fetch_array($resMeniu)): ?>
					<li><a href="<?echo $rowMen['link'];?>"><? echo $rowMen['numeLink']; ?></a></li>
					<? endwhile; ?>
				</ul>
			</div>
			<div id="content">
				<div id="contentWrap">
					<div class="block" style="height:60px;">
						<div style="width:380px; float:left;height:60px;">
							<div style="width:380px;height:40px;" >
								<b style="color:#00a652;font-size:18px;">Comanda Deschisa Nr. <? echo detaliFirma('serie_factura'). $nr_comanda; ?></b>
							</div>
								<div style="width:385px;float:left">
								<a class="buton" href="/comandanoua.php?nr_comanda=<? echo $nr_comanda; ?>&id_client=1&id_masina=1" >Sumar </a>&nbsp;
								<a class="buton" href="/comandanoua_simptome_constatari.php?id_comanda=<? echo $id_comanda; ?>&nr_comanda=<? echo $nr_comanda; ?>&id_client=1&id_masina=1&nr_inmat=<? echo @$_GET['nr_inmat']; ?>" >Simptome / Constatari </a>&nbsp;
								<a class="buton" href="/comandanoua_materiale.php?id_comanda=<? echo $id_comanda; ?>&nr_comanda=<? echo $nr_comanda; ?>&id_client=1&id_masina=1&nr_inmat=<? echo @$_GET['nr_inmat']; ?>" >Materiale </a>&nbsp;
								<a class="buton"  href="/comandanoua_reparatii.php?id_comanda=<? echo $id_comanda; ?>&nr_comanda=<? echo $nr_comanda; ?>&id_client=1&id_masina=1&nr_inmat=<? echo @$_GET['nr_inmat']; ?>"  >Reparatii </a>&nbsp;
							</div>
						</div>
					<div style="width:200px;height:55px;background:#707070;float:left;margin-left:180px;padding:4px;">
							<?
								/**************************** MATERIALE QUERY **************************/
									$res_pret_materiale = mysql_query("select pret,cant from comenzi_materiale,comenzi_materiale_relatii where comenzi_materiale.id_material = comenzi_materiale_relatii.id_material and id_comanda = '$id_comanda' ");
									$pret_materiale = array();
									while($rowPret = mysql_fetch_array($res_pret_materiale)){
										$pret_materiale[] = $rowPret['pret'] * $rowPret['cant']  ;
									}
									$pret_materiale = array_sum($pret_materiale);
								/**************************** MATERIALE QUERY **************************/	
									
								/**************************** REPARATI QUERY **************************/
									$res_pret_reparatii = mysql_query("select tarif,cant from comenzi_reparatii where  id_comanda = '$id_comanda' ");
									$pret_reparatii = array();
									while($rowPret = mysql_fetch_array($res_pret_reparatii)){
										$pret_reparatii[] = $rowPret['tarif'] * $rowPret['cant']  ;
									}
									$pret_reparatii = array_sum($pret_reparatii);
								/**************************** REPARATI QUERY **************************/
							?>
							Total materiale cu TVA: <? echo $pret_materiale; ?> RON<br/>
							
							Total Reparatii cu TVA <? echo $pret_reparatii; ?> RON<br/>
							<hr>
							<b>Total <? echo $pret_materiale + $pret_reparatii  ?> RON</b>
						</div>
					</div>
					
					<h2>Materiale Necesatre Comenzii </h2>
					
					<table>
						<tr id="capTabel">
							<td style="width:30px;">Cantitate</td>
							<td style="width:120px; text-align:center;">Cod Articol</td>
							<td style="width:400px; text-align:center;">Denumire</td>
							<td style="width:80px;  text-align:center;">Pret Unitar</td>
							<td style="width:80px; text-align:center;" >Valoare <small>fara TVA</small></td>
							<td style="width:80px; text-align:center;">Valoare <small>cu TVA (<? echo  detaliFirma('valoaretva') ; ?>%)</small></td>
							<td style="width:100px; text-align:center;"></td>
						</tr>
						<?
							$resM = mysql_query("select * from comenzi_materiale,comenzi_materiale_relatii where comenzi_materiale.id_material = comenzi_materiale_relatii.id_material and id_comanda = '$id_comanda'");
							while($rowMaterial = mysql_fetch_array($resM)):
						?>
						<tr style="height:30px;">
							<td style="width:30px;"><? echo $rowMaterial['cant']; ?></td>
							<td style="width:120px; text-align:center;"><? echo $rowMaterial['cod_articol']; ?></td>
							<td style="width:400px; text-align:center;"><? echo $rowMaterial['denumire']; ?></td>
							<?
								/*
								if($rowMaterial['tva'] == 0){
									$pret_unitar  = $rowMaterial['pret'];
									$val_fara_tva = $rowMaterial['pret'] * $rowMaterial['cant'];
									$val_cu_tva = cuTva($val_fara_tva);
								}else{
									$pret_unitar = faraTva($rowMaterial['pret']);
									$val_fara_tva = $pret_unitar * $rowMaterial['cant'];
									$val_cu_tva = cuTva($val_fara_tva);
								}
								*/
								$val_cu_tva =  $rowMaterial['pret'] * $rowMaterial['cant'];
								$val_fara_tva = faraTva($val_cu_tva);
							?>
							<td style="width:80px;  text-align:center;"><? echo $rowMaterial['pret']; ?></td>
							<td style="width:80px; text-align:center;" ><? echo $val_fara_tva;?></td>
							<td style="width:80px; text-align:center;"><? echo $val_cu_tva; ?></td>
							<td style="width:100px; text-align:center;"><a class="buton" href="/comandanoua_materiale.php?stergeMaterial=<? echo $rowMaterial['id_material']; ?>&id_comanda=<? echo $_GET['id_comanda'];?>&nr_comanda=<? echo $_GET['nr_comanda']; ?>&id_client=<? echo $_GET['id_client']; ?>&nr_inmat=<? echo $_GET['nr_inmat'];?>&id_masina=<? echo $_GET['id_masina'];?> ">sterge </a></td>
						</tr>
						<? endwhile; ?>
					</table>
					<div class="block"></div>
					
					<form action="" method="post">
						<div style="margin-left:50px; width:350px;height:120px;float:left;">
						<p>
							<label>Denumire articol</label>
							<input class="text" type="text" name="articol"  id="articol" value="">
						</p>
						<p>
							<label>Cantitate</label>
							<input class="text" type="text" name="cnt" value="">
						</p>
						
						</div>
						<div style="width:350px;height:120px;float:left;">
						<p>
							<label>Cod Articol</label>
							<input class="text" type="text" name="codarticol" id="cod_articol" value="">
						</p>
						<p>
							<label>
								Pret Unitar 
								cu TVA
							</label>
							<input class="text" type="text" name="pretu" value="">
						</p>
						</div>
						<p>
						<input class="buton" style="margin-left:300px;margin-top:10px" type="submit" value="Adauga Articol" name="add_articol">
						</p>
					</form>
					<div class="block" style="clear:both;"></div>
					<div id="results">
						
					</div>
				</div>
			</div>
		</div>
		<script src="js/jquery.js"></script>
		<script>
			$("#articol").keyup(function(){
				
				setTimeout(function(){
					cauta_produs = $("#articol").val();
					id_comanda = "<? echo $_GET['id_comanda']; ?>"
					nr_comanda = "<? echo $_GET['nr_comanda']; ?>"
					id_client = "<? echo $_GET['id_client']; ?>"
					nr_inmat = "<? echo $_GET['nr_inmat']; ?>"
					id_masina = "<? echo $_GET['id_masina']; ?>"
					$("#results").load("ajaxMateriale.php",{cauta:cauta_produs,id_comanda:id_comanda,nr_comanda:nr_comanda,id_client:id_client,nr_inmat:nr_inmat,id_masina:id_masina},function(data){
						$("#results").html(data);
					})
				},500)
				
			})
		</script>

	</body>
	
</html>